package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.common.constant.StaffConstants;
import com.xr.common.constant.WorkStatusConstants;
import com.xr.miniprogram.domain.MiniProgramOrder;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramServiceLog;
import com.xr.miniprogram.domain.MiniProgramStaff;

import com.xr.miniprogram.domain.vo.MiniProgramServiceLogVO;
import com.xr.miniprogram.domain.vo.MiniProgramStaffOrderVO;
import com.xr.miniprogram.mapper.*;
import com.xr.miniprogram.service.IMiniProgramOrderUserService;
import com.xr.miniprogram.service.IMiniProgramServiceLogService;
import com.xr.miniprogram.service.IMiniProgramStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.xr.common.utils.PageUtils.startPage;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author da
 * @since 2023-09-28
 */
@Service
public class MiniProgramStaffServiceImpl extends ServiceImpl<MiniProgramStaffMapper, MiniProgramStaff> implements IMiniProgramStaffService {
    @Autowired
    private MiniProgramStaffMapper mpStaffMapper;
    @Autowired
    private IMiniProgramServiceLogService mpServiceLogService;
    @Autowired
    private IMiniProgramOrderUserService mpOrderUserService;
    @Autowired
    private MiniProgramOrderMapper mpOrderMapper;
    @Autowired
    private MiniProgramOrderUserMapper orderUserMapper;

    @Override
    public List<MiniProgramStaffOrderVO> getStaffOrderList(Long userId, String date, Long workStatus) {
        //userId查询staff staffId查询serviceLog serviceLogId查询orderUser orderUserId查询order
        MiniProgramStaff miniProgramStaff = this.getByUserId(userId);
        Long staffId = miniProgramStaff.getStaffId();
        List<MiniProgramServiceLog> serviceLogList = mpServiceLogService.listByStaffId(staffId);
        ArrayList<MiniProgramStaffOrderVO> listStaffOrderVO = new ArrayList<>();
        for (MiniProgramServiceLog serviceLog: serviceLogList ) {
            MiniProgramOrderUser orderUser = orderUserMapper.getByServiceTime(serviceLog.getServiceLogId(),date, workStatus);
            if (orderUser != null) {
                MiniProgramOrder miniProgramOrder = mpOrderMapper.selectById(orderUser.getOrderId());
                listStaffOrderVO.add(new MiniProgramStaffOrderVO(miniProgramOrder, orderUser, MiniProgramServiceLogVO.toMiniProgramServiceLogVO(serviceLog)));
            }
        }
        return listStaffOrderVO;
    }

    @Override
    public MiniProgramStaffOrderVO getStaffOrderByServiceLogId(Long serviceLogId) {
        MiniProgramServiceLog serviceLog = mpServiceLogService.getById(serviceLogId);
        MiniProgramOrderUser orderUser = orderUserMapper.getByServiceTime(serviceLog.getServiceLogId(),null, null);
        MiniProgramOrder miniProgramOrder = null;
        if (orderUser != null) {
            miniProgramOrder = mpOrderMapper.selectById(orderUser.getOrderId());
        }
        return new MiniProgramStaffOrderVO(miniProgramOrder, orderUser, MiniProgramServiceLogVO.toMiniProgramServiceLogVO(serviceLog));
    }

    @Override
    public MiniProgramStaff getByUserId(Long userId) {
        return this.getOne(new QueryWrapper<MiniProgramStaff>().lambda().eq(MiniProgramStaff::getUserId, userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStaffStatus(Long staffId, Long status) {
        MiniProgramStaff staff = new MiniProgramStaff();
        staff.setStaffId(staffId);
        staff.setStaffStatus(status);
        return this.updateById(staff);
    }

    @Override
    public Boolean updateStaffOrder(Long serviceLogId, Long newStaffId) {
        MiniProgramServiceLog serviceLog = mpServiceLogService.getById(serviceLogId);
        //如果原staff下修改后没有分配，更改其分配状态
        Long staffId = serviceLog.getStaffId();
        int count = mpServiceLogService.count(new QueryWrapper<MiniProgramServiceLog>().lambda().eq(MiniProgramServiceLog::getStaffId , staffId).ne(MiniProgramServiceLog::getWorkStatus , WorkStatusConstants.END_SERVICE));
        if(count == 1){
            this.updateStaffStatus(staffId, StaffConstants.UNALLOCATED);
        }
        serviceLog.setStaffId(newStaffId);
        return mpServiceLogService.updateById(serviceLog);
    }

    /**
     * 查询服务工人
     *
     * @param staffId 服务工人主键
     * @return 服务工人
     */
    @Override
    public MiniProgramStaff selectMiniProgramStaffByStaffId(Long staffId)
    {
        return mpStaffMapper.selectMiniProgramStaffByStaffId(staffId);
    }

    /**
     * 查询服务工人列表
     *
     * @param mpStaff 服务工人
     * @return 服务工人
     */
    @Override
    public List<MiniProgramStaff> selectMiniProgramStaffList(MiniProgramStaff mpStaff)
    {
        return mpStaffMapper.selectMiniProgramStaffList(mpStaff);
    }

    /**
     * 新增服务工人
     *
     * @param mpStaff 服务工人
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMiniProgramStaff(MiniProgramStaff mpStaff)
    {
        return mpStaffMapper.insertMiniProgramStaff(mpStaff);
    }

    /**
     * 修改服务工人
     *
     * @param mpStaff 服务工人
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMiniProgramStaff(MiniProgramStaff mpStaff)
    {
        return mpStaffMapper.updateMiniProgramStaff(mpStaff);
    }

    /**
     * 批量删除服务工人
     *
     * @param staffIds 需要删除的服务工人主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteMiniProgramStaffByStaffIds(Long[] staffIds)
    {
        return mpStaffMapper.deleteMiniProgramStaffByStaffIds(staffIds);
    }

    /**
     * 删除服务工人信息
     *
     * @param staffId 服务工人主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteMiniProgramStaffByStaffId(Long staffId)
    {
        return mpStaffMapper.deleteMiniProgramStaffByStaffId(staffId);
    }

    @Override
    public List<HashMap<String, String>> getCountByDate(Long userId) {
        MiniProgramStaff miniProgramStaff = this.getByUserId(userId);
        Long staffId = miniProgramStaff.getStaffId();
        return mpStaffMapper.getCount(staffId);
    }
}
